package com.sky.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper  shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //1、根据菜品ID和菜品口味   或者   套餐ID查询 该商品是否存在
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> resutList = this.shoppingCartMapper.queryByParams(shoppingCart);

        //2、如果存在  数量+1
        if(ObjectUtil.isNotEmpty(resutList)){
            shoppingCart = resutList.get(0);
            // update shopping_cart set number = number + 1 where id = ?
            this.shoppingCartMapper.incremetNumById(shoppingCart.getId());
            return;
        }

        //3、如果不存在  保存
        //3.1、如果菜品ID不为空，证明当前添加的是菜品
        if(ObjectUtil.isNotEmpty(shoppingCartDTO.getDishId())){
            DishVO dish = dishMapper.getByIdWithFlavor(shoppingCartDTO.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }else{
            //3.2、如果菜品ID为空，证明当前添加的是套餐
            SetmealVO setmeal = setmealMapper.queryById(shoppingCartDTO.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }

        shoppingCart.setNumber(1);
        this.shoppingCartMapper.save(shoppingCart);

    }

    /**
     * 查询购物车列表
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        return this.shoppingCartMapper.queryByParams(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }


    @Override
    public void updates(ShoppingCartDTO shoppingCartDTO) {
        //1、根据菜品ID和菜品口味   或者   套餐ID查询 该商品是否存在
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> resutList = this.shoppingCartMapper.queryByParams(shoppingCart);

        //2、如果数量大于1则-1
        if (ObjectUtil.isNotEmpty(resutList)) {
            shoppingCart = resutList.get(0);
            if (shoppingCart.getNumber() > 1) {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                this.shoppingCartMapper.updates(shoppingCart.getId());
            } else {
                this.shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
            }
        }
    }
}
